package org.dromara.canteen.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.excel.EasyExcel;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.canteen.listener.FoodImportExcelListener;
import org.dromara.school.service.ICampusService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.canteen.domain.vo.CanteenFoodVo;
import org.dromara.canteen.domain.bo.CanteenFoodBo;
import org.dromara.canteen.service.ICanteenFoodService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 食材管理
 *
 * @author Gjb
 * @date 2025-07-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/canteen/food")
public class CanteenFoodController extends BaseController {

    private final ICanteenFoodService canteenFoodService;

    private final ICampusService campusService;
    /**
     * 查询食材管理列表
     */
    @SaCheckPermission("canteen:food:list")
    @GetMapping("/list")
    public TableDataInfo<CanteenFoodVo> list(CanteenFoodBo bo, PageQuery pageQuery) {
        return canteenFoodService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出食材管理列表
     */
    @SaCheckPermission("canteen:food:export")
    @Log(title = "食材管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CanteenFoodBo bo, HttpServletResponse response) {
        List<CanteenFoodVo> list = canteenFoodService.queryList(bo);
        ExcelUtil.exportExcel(list, "食材管理", CanteenFoodVo.class, response);
    }

    /**
     * 获取食材管理详细信息
     *
     * @param foodId 主键
     */
    @SaCheckPermission("canteen:food:query")
    @GetMapping("/{foodId}")
    public R<CanteenFoodVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long foodId) {
        return R.ok(canteenFoodService.queryById(foodId));
    }

    /**
     * 新增食材管理
     */
    @SaCheckPermission("canteen:food:add")
    @Log(title = "食材管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CanteenFoodBo bo) {
        return toAjax(canteenFoodService.insertByBo(bo));
    }

    /**
     * 修改食材管理
     */
    @SaCheckPermission("canteen:food:edit")
    @Log(title = "食材管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CanteenFoodBo bo) {
        return toAjax(canteenFoodService.updateByBo(bo));
    }

    /**
     * 删除食材管理
     *
     * @param foodIds 主键串
     */
    @SaCheckPermission("canteen:food:remove")
    @Log(title = "食材管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{foodIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] foodIds) {
        return toAjax(canteenFoodService.deleteWithValidByIds(List.of(foodIds), true));
    }





    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "食材导入模版", CanteenFoodVo.class, response);
    }





    @PostMapping("/upload")
    public R upload(@RequestPart("file") MultipartFile file) {

        try {
            EasyExcel.read(file.getInputStream(), CanteenFoodVo.class, new FoodImportExcelListener(canteenFoodService,campusService)).sheet().doRead();
            return R.ok();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return R.fail("文件导入失败！");
    }









}
